home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDEMO.PAK / FONTX.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  170 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //
  5. //   Font demo window for GDIDemo program
  6. //----------------------------------------------------------------------------
  7. #include <owl/pch.h>
  8. #include <owl/applicat.h>
  9. #include <owl/dc.h>
  10. #include <owl/scroller.h>
  11. #include <string.h>
  12. #include "fontx.h"
  13.  
  14. //
  15. //
  16. //
  17. struct FontInfoRec {
  18.   TFont* Font;     // Logical font object
  19.   int    Height;   // Height of logical font in pixels
  20.   int    Width;    // Width of name of the font in pixels
  21.   char   Name[LF_FACESIZE];  // Name of this font
  22. };
  23.  
  24. //
  25. // Local variables used by EnumerateFonts callback function
  26. //
  27. static int         FontUsers = 0;
  28. static FontInfoRec FontInfo[MaxNumFonts];
  29. static int         NumFonts;            // Number of system fonts available
  30. static TDC*        TheDC = 0;
  31.  
  32. //
  33. // EnumerateFont is a call back function.  It receives information
  34. //  about system fonts.  It creates an example of each font by calling
  35. //  CreateFont. When MaxNumFonts have been processed, 0 is returned
  36. //  notifying windows to stop sending information, otherwise 1 is
  37. //  returned telling windows to send more information if available
  38. //
  39. #if defined(BI_PLAT_WIN32)
  40. int __stdcall
  41. #else
  42. int far pascal __export
  43. #endif
  44. EnumerateFont(LOGFONT far* logFont, TEXTMETRIC far*, int, LPARAM)
  45. {
  46.   // Create the font described by logFont
  47.   //
  48.   FontInfo[NumFonts].Font = new TFont(logFont);
  49.  
  50.   // Save the height of the font for positioning when drawing in the window
  51.   //
  52.   FontInfo[NumFonts].Height = logFont->lfHeight;
  53.  
  54.   // Save the name of the font for drawing in the window
  55.   //
  56.   strcpy(FontInfo[NumFonts].Name, logFont->lfFaceName);
  57.   TheDC->SelectObject(*FontInfo[NumFonts].Font);
  58.  
  59.   TSize extent(0,0);
  60.   TheDC->GetTextExtent(logFont->lfFaceName, strlen(logFont->lfFaceName),
  61.                        extent);
  62.   FontInfo[NumFonts].Width = extent.cx;
  63.   TheDC->RestoreFont();
  64.   NumFonts++;
  65.  
  66.   return NumFonts >= MaxNumFonts ?
  67.       0 :      // Don't send any more information, the array is full
  68.       1;       // Send more information if available
  69. }
  70.  
  71. //
  72. // Collect all of the system fonts
  73. //
  74. void
  75. GetFontInfo()
  76. {
  77.   if (FontUsers == 0) {
  78.     TheDC = new TScreenDC;
  79.     NumFonts = 0;
  80.  
  81.     // Create an instance of the call back function.  This allows
  82.     // our program to refer to an exported function.  Otherwise the
  83.     // Data segment will not be correct. This is a no-op in 32bit.
  84.     //
  85.     TProcInstance enumProc((FARPROC)EnumerateFont);
  86.  
  87.     // Gather information about all fonts that are allowable in our DC
  88.     //
  89.     EnumFonts(*TheDC, 0, (OLDFONTENUMPROC)(FARPROC)enumProc, 0);
  90.  
  91.     delete TheDC;
  92.     TheDC = 0;
  93.   }
  94.   FontUsers++;
  95. }
  96.  
  97. //
  98. // Release font information
  99. //
  100. void
  101. ReleaseFontInfo()
  102. {
  103.   FontUsers--;
  104.   if (FontUsers == 0)
  105.     for (int i = 0; i < NumFonts; i++) {
  106.       delete FontInfo[i].Font;
  107.       FontInfo[i].Font = 0;
  108.     }
  109. }
  110.  
  111. // TFontWindow ----------------------------------------------------
  112.  
  113. DEFINE_RESPONSE_TABLE1(TFontWindow, TBaseDemoWindow)
  114.   EV_WM_SIZE,
  115. END_RESPONSE_TABLE;
  116.  
  117. IMPLEMENT_CASTABLE1(TFontWindow, TBaseDemoWindow);
  118.  
  119. //
  120. // Initialize object and collect font information
  121. //
  122. TFontWindow::TFontWindow() : TBaseDemoWindow()
  123. {
  124.   GetFontInfo();
  125.   Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  126.   FontsHeight = 0;
  127.   FontsWidth = 0;
  128.   for (int i = 0; i < NumFonts; i++) {
  129.     FontsHeight += FontInfo[i].Height;
  130.     if (FontsWidth < FontInfo[i].Width)
  131.       FontsWidth = FontInfo[i].Width;
  132.   }
  133.   Scroller = new TScroller(this, 1, 1, 0, 0);
  134. }
  135.  
  136. //
  137. //
  138. //
  139. TFontWindow::~TFontWindow()
  140. {
  141.   ReleaseFontInfo();
  142. }
  143.  
  144. //
  145. // Draw each font name in it's font in the Display context.  Each
  146. //  line is incremented by the height of the font
  147. //
  148. void
  149. TFontWindow::Paint(TDC& dc, bool, TRect&)
  150. {
  151.   TPoint position(10,0);
  152.   for (int i = 0; i < NumFonts; i++) {
  153.     dc.SelectObject(*FontInfo[i].Font);
  154.     dc.TextOut(position, FontInfo[i].Name, strlen(FontInfo[i].Name));
  155.     position.Offset(0, FontInfo[i].Height);
  156.   }
  157. }
  158.  
  159. //
  160. //
  161. //
  162. void
  163. TFontWindow::EvSize(uint SizeType, TSize& Size)
  164. {
  165.   TBaseDemoWindow::EvSize(SizeType, Size);
  166.   if (Scroller)
  167.     Scroller->SetRange(FontsWidth - Size.cx + 10,
  168.                        FontsHeight - Size.cy);
  169. }
  170.